/* 弱引用:
        1.弱引用对象的存在不会阻止它所指向的对象被垃圾回收器回收; 
        2.弱引用最常见的用途是实现规范映射(Map中Key为对象的情况);
        3.弱引用解决循环引用问题; 
*/


/*Set的API: add has delete clear */
{
    let arr = ['add', 'delete', 'clear', 'has'];
    let list = new Set(arr);
    list.add('Hahaha');
    console.log('has', list.has('add'));
    console.log('delete', list.delete('add'), list);
    list.clear();
    console.log('list', list);
}

/* 数组转set */
{
    let arr = [1, 2, 3, 4, 5];
    let list = new Set(arr);
    console.log('size', list.size);
}

/* 数字字符串和数值在set中是不同的数据 */
{
    let list = new Set();
    list.add(1);
    list.add(2);
    list.add(1);
    console.log('list', list);
    let arr = [1, 2, 3, 1, '2'];
    let list2 = new Set(arr);
    console.log('unique', list2);
}


/* 遍历Set: keys values entries */
{
    let arr = ['add', 'delete', 'clear', 'has'];
    let list = new Set(arr);

    for (let key of list.keys()) {
        console.log('keys', key);
    }
    for (let value of list.values()) {
        console.log('value', value);
    }
    for (let [key, value] of list.entries()) {
        console.log('entries', key, value);
    }
    list.forEach(function (item) { console.log(item); })
}


/* WeakSet: 没有size; key必须是引用对象不能是值 */
{
    let weakList = new WeakSet();
    let arg = {};
    weakList.add(arg);
    console.log('weakList', weakList);
}


/* map的API: set get size delete clear */
{
    let map = new Map([['a', 123], ['b', 456]]);
    console.log('map args', map);
    console.log('add', map.set('c', 789));
    console.log('get', map.get('a'));
    console.log('size', map.size);
    console.log('delete', map.delete('a'), map);
    console.log('clear', map.clear(), map);
}

/* WeakMap */
{
    let weakmap = new WeakMap();
    let o = {};
    weakmap.set(o, 123);
    console.log(weakmap.get(o));
}